###############################################
#                                             #
#     National Survey Treatment Effects       #
#           Appendix Models                   #
#     Created by Mac Lockhart Mar 27 2023     #
#     Updated Mar 27 2023                     #
#                                             #
###############################################

library(tidyverse)
library(data.table)
library(modelsummary)
library(kableExtra)
library(gt)
library(flextable)
library(car)

#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}

DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre
DF$votefraud <- DF$votefraudpost - DF$votefraudpre
DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
DF$vote2024 <- DF$vote2024post - DF$vote2024pre

#Subset to correct respondents who
#got the treatment manipulation and viewed the video for 20 seconds
x<-cbind(DF$`Emotions timer_Page Submit`,DF$`Facts timing_Page Submit`, DF$`Nat control timing_Page Submit`)
DF$viewing<- rowSums(x, na.rm=T)
rm(x)
DF$viewed_20 <- ifelse(DF$viewing>20, 1, 0)
x<-cbind(DF$`Nat Control Manipula`=="State Farm",DF$`Emotion Manipulation`=="Virginia",DF$`Fact manipulation`=="Maricopa")
DF$manip<- rowSums(x, na.rm=T)
rm(x)

#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment, subset(DF, DF$manip==1 & DF$viewed_20==1))
m1.2 <- lm(otherstates ~ d_treatment, subset(DF, DF$manip==1 & DF$viewed_20==1))
m1.3 <- lm(votefraud ~ d_treatment, subset(DF, DF$manip==1 & DF$viewed_20==1))
m1.4 <- lm(officialfraud ~ d_treatment, subset(DF, DF$manip==1 & DF$viewed_20==1))
m1.5 <- lm(vote2024 ~ d_treatment, subset(DF, DF$manip==1 & DF$viewed_20==1))

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/national_binary_attentive.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


rm(m1.1, m1.2, m1.3, m1.4, m1.5)



#Subset to respondents with moderate levels of trust ex-ante
x<-cbind(DF$ownstatepre==4, DF$otherstatepre==4, DF$votefraudpre==5, DF$officialfraudpre==5)
DF$high_trust<- ifelse(rowSums(x, na.rm=T)==4, 1, 0)
rm(x)
x<-cbind(DF$ownstatepre==1, DF$otherstatepre==1, DF$votefraudpre==1, DF$officialfraudpre==1)
DF$low_trust<- ifelse(rowSums(x, na.rm=T)==4, 1, 0)
rm(x)


#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment, subset(DF, DF$low_trust==0 & DF$high_trust==0))
m1.2 <- lm(otherstates ~ d_treatment, subset(DF, DF$low_trust==0 & DF$high_trust==0))
m1.3 <- lm(votefraud ~ d_treatment, subset(DF, DF$low_trust==0 & DF$high_trust==0))
m1.4 <- lm(officialfraud ~ d_treatment, subset(DF, DF$low_trust==0 & DF$high_trust==0))
m1.5 <- lm(vote2024 ~ d_treatment, subset(DF, DF$low_trust==0 & DF$high_trust==0))

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/national_binary_persuadable.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


rm(m1.1, m1.2, m1.3, m1.4, m1.5)
